package com.example.memolaga.accounts;

import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.preference.PreferenceManager;
import android.text.InputType;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.memolaga.Constants;
import com.example.memolaga.R;
import com.example.memolaga.password.FunctionsPass;

public class AccView extends Activity {

    private String sKey;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.acc_view);
        String retrievedData = "";
        Bundle bundle = this.getIntent().getExtras();
        if(bundle != null) {
            SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
            if (preferences.getBoolean(Constants.getPrefStringShowPass(), Constants.getPrefDefaultShowPass()))
            {
                ((ImageView) findViewById(R.id.imgViewUserAcc)).setVisibility(View.GONE);
                ((TextView) findViewById(R.id.txtViewAccUsername)).setInputType(InputType.TYPE_CLASS_NUMBER);
                ((ImageView) findViewById(R.id.imgViewPassAcc)).setVisibility(View.GONE);
                ((TextView) findViewById(R.id.txtViewAccPass)).setInputType(InputType.TYPE_CLASS_NUMBER);
            }
            retrievedData = bundle.getString(Constants.getCardsName());
            this.sKey = bundle.getString(Constants.getKeyString());
            ((TextView) findViewById(R.id.txtViewAccName)).setText(retrievedData);
            AccountsMaster acc = AccountsManager.getInstance().getAccount(retrievedData);
            ((TextView) findViewById(R.id.txtViewAccServer)).setText(acc.getServer());
            ((TextView) findViewById(R.id.txtViewAccPath)).setText(acc.getPath());
            ((TextView) findViewById(R.id.txtViewAccUsername)).setText(FunctionsPass.decryptString(sKey, acc.getUsername()));
            ((TextView) findViewById(R.id.txtViewAccPass)).setText(FunctionsPass.decryptString(sKey, acc.getPassword()));
            ((TextView) findViewById(R.id.txtViewAccProvider)).setText(acc.getProvider());
            ((TextView) findViewById(R.id.txtViewAccProvWeb)).setText(acc.getProviderWebSite());
            ((TextView) findViewById(R.id.txtViewAccProvPhone)).setText(acc.getProviderPhone());
            ((TextView) findViewById(R.id.txtViewAccProvPhone)).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent callIntent = new Intent(Intent.ACTION_CALL);
                    callIntent.setData(Uri.parse("tel:+" + ((TextView) findViewById(R.id.txtViewAccProvPhone)).getText().toString().trim()));
                    startActivity(callIntent);
                }
            });
            ((TextView) findViewById(R.id.txtViewAccNote)).setText(acc.getNote());
            ((ImageView) findViewById(R.id.imgViewUserAcc)).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (((TextView) findViewById(R.id.txtViewAccUsername)).getInputType()!=InputType.TYPE_CLASS_NUMBER)
                    {
                        ((ImageView) findViewById(R.id.imgViewUserAcc)).setImageDrawable(getResources().getDrawable(R.drawable.secure2));
                        ((TextView) findViewById(R.id.txtViewAccUsername)).setInputType(InputType.TYPE_CLASS_NUMBER);
                    }
                    else
                    {
                        ((ImageView) findViewById(R.id.imgViewUserAcc)).setImageDrawable(getResources().getDrawable(R.drawable.not_secure2));
                        ((TextView) findViewById(R.id.txtViewAccUsername)).setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD);
                    }
                }
            });
            ((ImageView) findViewById(R.id.imgViewPassAcc)).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (((TextView) findViewById(R.id.txtViewAccPass)).getInputType()!=InputType.TYPE_CLASS_NUMBER)
                    {
                        ((ImageView) findViewById(R.id.imgViewPassAcc)).setImageDrawable(getResources().getDrawable(R.drawable.secure2));
                        ((TextView) findViewById(R.id.txtViewAccPass)).setInputType(InputType.TYPE_CLASS_NUMBER);
                    }
                    else
                    {
                        ((ImageView) findViewById(R.id.imgViewPassAcc)).setImageDrawable(getResources().getDrawable(R.drawable.not_secure2));
                        ((TextView) findViewById(R.id.txtViewAccPass)).setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD);
                    }
                }
            });
        }
        Bundle b = new Bundle();
        b.putString("Value", retrievedData);
        Intent intent = new Intent();
        intent.putExtras(b);
        setResult(Constants.getResultNoEdit(), intent);
    }


/*
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.acc_view, menu);
        return true;
    }
    */
}
